﻿using ConmonDateModel;
using MyLogProject;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateDBControl
{
    public class SqlManageControl
    {
        private string SqlConnString { set; get; }

        private MySqlConnection Conn { set; get; }

        public SqlManageControl()
        {
            LoadDBConfig();
        }

        #region DBLoad
        public void LoadDBConfig()
        {
            try
            {
                string FloderStr = AppDomain.CurrentDomain.BaseDirectory + "MysqlConfig";
                if (!Directory.Exists(FloderStr))
                {
                    Directory.CreateDirectory(FloderStr);
                }
                string dbStr = AppDomain.CurrentDomain.BaseDirectory + "MysqlConfig//DBInfo.json";
                if (!File.Exists(dbStr))
                {
                    var item = new DBInfo();
                    item.ServerAddress = "localhost";
                    item.DataBase = "webdata";
                    item.UserName = "root";
                    item.PassWord = "lcl.931202";
                    item.IsNeddLogin = true;
                    var itemContent = JsonConvert.SerializeObject(item);
                    File.WriteAllText(dbStr, itemContent);
                }
                string strContent = File.ReadAllText(dbStr);
                var itemModel = JsonConvert.DeserializeObject<DBInfo>(strContent);
                SqlConnString = $"server = {itemModel.ServerAddress}; database = {itemModel.DataBase}; username = {itemModel.UserName}; password = {itemModel.PassWord};Charset=utf8;";
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "LoadDBConfig", ex.ToString());
            }
        }


        public bool ConnectDB()
        {
            bool flg = false;
            try
            {
                Conn = new MySqlConnection(SqlConnString);
                Conn.Open();
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "ConnectDB", ex.ToString());
            }
            return flg;
        }

        private void CloseDB()
        {
            try
            {
                Conn?.Close();
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "CloseDB", ex.ToString());
            }
        }
        #endregion

        #region Do
        /// <summary>
        /// Get
        /// </summary>
        /// <param name="_sql"></param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string _sql)
        {
            DataTable data = new DataTable();
            using (MySqlConnection Conn = new MySqlConnection(SqlConnString))
            {
                Conn.Open();

                MySqlCommand command = new MySqlCommand(_sql, Conn);
                using (var sdr = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    data.Load(sdr);
                }
            }
            return data;
        }



        /// <summary>
        /// Update/insert/delete
        /// </summary>
        /// <param name="_sql"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string _sql)
        {
            int result = 0;
            using (MySqlConnection Conn = new MySqlConnection(SqlConnString))
            {
                Conn.Open();
                MySqlCommand command = new MySqlCommand(_sql, Conn);
                result = command.ExecuteNonQuery();
                Conn.Close();
                Conn.Dispose();
                command.Dispose();
            }
            return result;
        }

        public int ExeCuteCommonSql(string _sql)
        {
            int result = -1;
            try
            {
                using (MySqlConnection Conn = new MySqlConnection(SqlConnString))
                {
                    Conn.Open();
                    MySqlCommand command = new MySqlCommand(_sql, Conn);
                    result = command.ExecuteNonQuery();
                    Conn.Close();
                    Conn.Dispose();
                    command.Dispose();
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "ExeCuteCommonSql", ex.ToString());
            }
            return result;
        }


        public int GetCommonDateCount(string _sql)
        {
            int num = 0;
            DataTable data = new DataTable();
            try
            {
                using (MySqlConnection Conn = new MySqlConnection(SqlConnString))
                {
                    Conn.Open();

                    MySqlCommand command = new MySqlCommand(_sql, Conn);
                    using (var sdr = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        data.Load(sdr);
                        if (data.Rows.Count > 0)
                        {
                            num = Convert.ToInt32(data.Rows[0].ItemArray[0]);

                        }
                    }
                }

            }
            catch (Exception ex)
            {
               
                LogManager.WriteLog(LogTypeEnum.Exception, "ExecuteQuery", ex.ToString());
            }
            return num;
        }


      
        #endregion
    }
}
